package cn.edu.ctbu.lddweb.controller;

import cn.edu.ctbu.lddweb.pojo.Vacation;
import cn.edu.ctbu.lddweb.service.VacationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 教师审核所教授学生请假情况
 */
@Controller
public class ExamineController {
    @Autowired
    private VacationService vacationService;

    //当前教师教授学生学生请假情况
    @RequestMapping("/examine/index")
    public String index(Model model, String username, ModelMap map,Vacation vacation){
        vacation.setTeacherName(username);
        List<Vacation> vacations = vacationService.selectStudentName(vacation);
        model.addAttribute("vacation",vacations);

        map.put("username",username);
        return "/examine/index";
    }

    //搜索/重置
    @RequestMapping("/selectVacation1")
    public String selectVacation1(@RequestParam(value = "type",required = false) String type, @RequestParam(value = "isAgree",required = false) String isAgree, Model model){
        Vacation vacation = new Vacation();
        vacation.setType(type);
        vacation.setIsAgree(isAgree);
        List<Vacation> list = vacationService.selectStudentName(vacation);
        model.addAttribute("vacation",list);
        return "/examine/index";
    }

    //修改
    @RequestMapping("/examine/edit")
    public String edit(int id,Model model,String username,ModelMap map){
        Vacation vacation = vacationService.findID(id);
        model.addAttribute("vacation",vacation);

        map.put("username",username);
        return "/examine/edit";
    }

    //修改操作
    @RequestMapping("/updateVacation1")
    public String updateVacation1(Vacation vacation){
        //获取当前时间并且格式化
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义新的日期格式
        String dateString = formatter.format(date);
        vacation.setExamineDate(dateString);//当前教师审核时间
        vacationService.updateVacationbyTeacher(vacation);
        return "/examine/index";
    }
}
